
class MyThread extends Thread{
    @Override
    public void run() {
        while(true){
            System.out.println("MyThread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

class TestDemo{
    public int count=0;

    public  void fun(){
        for (int i = 0; i < 500; i++) {
            count++;
        }
    }
}
public class Test {

    public static TestDemo testDemo=new TestDemo();
    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            for (int i = 0; i < 500; i++) {
                testDemo.fun();
            }
        });

/*        Thread t2=new Thread(()->{
            for (int i = 0; i < 500; i++) {
                testDemo.fun();
            }
        });*/

        t1.start();

        System.out.println(testDemo.count);

    }


    public static void main5(String[] args) {

        Thread t=new Thread(){
            @Override
            public void run() {
                System.out.println("Thread");
            }
        };
        t.interrupt();

    }
    public static void main4(String[] args) {
        Thread t=new Thread(new Thread(){
            @Override
            public void run() {
                while (true) {
                    System.out.println("Thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        },"这是俺的线程");

        t.start();

        try {
            t.join(1000,3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (true) {
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
    public static void main3(String[] args) {
        Thread t=new Thread(new Thread(){
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println("hello");
                }
            }
        },"这是俺的线程");

        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t.interrupt();
        System.out.println("主线程结束");
    }
    public static void main2(String[] args){
        Thread t=new Thread(new Thread(){
            @Override
            public void run(){
                try {
                    while (true){
                        System.out.println("hello");
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"这是俺的线程");
        t.setDaemon(true);
        t.start();

        System.out.println("主线程结束");
    }
    public static void main1(String[] args) {
       MyThread myThread=new MyThread();
       myThread.start();
       while (true){
           System.out.println("main");
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
    }
}
